package topin.facecat.FaceCat.UI

import facecat.topin.btn.FCButton
import facecat.topin.core.FCColor
import facecat.topin.core.FCFont
import facecat.topin.core.FCPaint
import facecat.topin.core.FCPoint
import facecat.topin.core.FCRect
import facecat.topin.tab.FCTabView

class RibbonButton : FCButton() {
    private val m_arrowType = 0

    init {
        backColor = FCColor.None
        borderColor = FCColor.None
        font = FCFont("Default", 12f, false, false, false)
    }

    val isSelected: Boolean
        get() {
            val parent = parent
            if (parent != null) {
                val tabView = parent as? FCTabView
                if (tabView != null) {
                    val selectedTabPage = tabView.selectedTabPage
                    if (selectedTabPage != null) {
                        if (this === selectedTabPage.headerButton) {
                            return true
                        }
                    }
                }
            }
            return false
        }

    override fun onPaintBackground(paint: FCPaint, clipRect: FCRect) {
//        FCNative native = getNative();
        val width = width
        val height = height
        val mw = width / 2
        val mh = height / 2
        var drawRect = FCRect(0, 0, width, height)
        var cornerRadius = 0
        var isTabHeader = false
        cornerRadius = 0
        if (m_arrowType > 0) {
            cornerRadius = 0
        }
        val parent = parent
        if (parent != null) {
            if (parent as? FCTabView != null) {
                cornerRadius = 0
                isTabHeader = true
            }
        }
        if (isTabHeader) {
            drawRect = FCRect(drawRect.left, 1, drawRect.right, drawRect.bottom - 1)
            if (MyColor.m_style == 1) {
                if (isSelected) {
                    paint.fillRect(FCColor.rgb(150, 150, 150), drawRect)
                } else {
                    paint.fillRect(FCColor.rgb(200, 200, 200), drawRect)
                    paint.drawLine(
                        FCColor.rgb(70, 76, 79),
                        1f,
                        0,
                        width - 3,
                        height / 4,
                        width - 3,
                        height * 3 / 4
                    )
                }
            } else {
                if (isSelected) {
                    paint.fillRect(FCColor.rgb(75, 75, 75), drawRect)
                } else {
                    paint.fillRect(FCColor.rgb(50, 50, 50), drawRect)
                    paint.drawLine(
                        FCColor.rgb(70, 76, 79),
                        1f,
                        0,
                        width - 3,
                        height / 4,
                        width - 3,
                        height * 3 / 4
                    )
                }
            }
        } else {
            cornerRadius = 0
            if (m_arrowType > 0) {
                val point1 = FCPoint()
                val point2 = FCPoint()
                val point3 = FCPoint()
                val dSize = Math.min(mw, mh) / 2
                when (m_arrowType) {
                    1 -> {
                        point1.x = mw - dSize
                        point1.y = mh
                        point2.x = mw + dSize
                        point2.y = mh - dSize
                        point3.x = mw + dSize
                        point3.y = mh + dSize
                    }

                    2 -> {
                        point1.x = mw + dSize
                        point1.y = mh
                        point2.x = mw - dSize
                        point2.y = mh - dSize
                        point3.x = mw - dSize
                        point3.y = mh + dSize
                    }

                    3 -> {
                        point1.x = mw
                        point1.y = mh - dSize
                        point2.x = mw - dSize
                        point2.y = mh + dSize
                        point3.x = mw + dSize
                        point3.y = mh + dSize
                    }

                    4 -> {
                        point1.x = mw
                        point1.y = mh + dSize
                        point2.x = mw - dSize
                        point2.y = mh - dSize
                        point3.x = mw + dSize
                        point3.y = mh - dSize
                    }
                }
                val points = arrayOfNulls<FCPoint>(3)
                points[0] = point1
                points[1] = point2
                points[2] = point3
                paint.fillPolygon(FCColor.Text, points)
            }
            if (m_arrowType == 0) {
                var state = false
                if (this === m_native.pushedView) {
                    state = true
                    paint.fillRoundRect(FCColor.rgba(255, 255, 255, 25), drawRect, cornerRadius)
                } else if (this === m_native.hoveredView) {
                    state = true
                    paint.fillRoundRect(FCColor.rgba(0, 0, 0, 25), drawRect, cornerRadius)
                }
                if (state) {
                    if (cornerRadius > 0) {
                        paint.drawRoundRect(
                            FCColor.rgb(255, 255, 255),
                            2f,
                            0,
                            drawRect,
                            cornerRadius
                        )
                    } else {
                        paint.drawRect(FCColor.rgb(255, 255, 255), 1f, 0, drawRect)
                    }
                }
            }
        }
    }
}


